//
//  RootViewController.m
//  vidio
//
//  Created by xucheng on 15/4/9.
//  Copyright (c) 2015年 xuchenghui. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //利用自带MPMoviePlayerController来实现视频播放,首先要在 项目中导入MediaPlayer.Framework框架包.
    //在试图控制器中导入#import "MediaPlayer/MPMoviePlayerController.h"
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
    imageView.image = [UIImage imageNamed:@"00013"];
    [self.view addSubview:imageView];
    [imageView release];
    NSLog(@"哈哈哈哈");      
    // 播放视频按钮
    UIButton *playButton = [UIButton buttonWithType:UIButtonTypeSystem];
    playButton.frame = CGRectMake(200, 230, 100, 30);
    [playButton addTarget:self action:@selector(playClick:) forControlEvents:UIControlEventTouchUpInside];
    [playButton setTitle:@"播放视频" forState:UIControlStateNormal];
    
    playButton.backgroundColor = [UIColor greenColor];
    playButton.layer.cornerRadius = 5;
    playButton.layer.masksToBounds = YES;
    [self.view addSubview:playButton];
}
- (void)playClick:(UIButton *)btn
{
    //视频文件路径,此视频已经存入项目包中.属于本地播放
    NSString *path = [[NSBundle mainBundle] pathForResource:@"9533522808.f4v" ofType:@"mp4"];
    //视频URL
    NSURL *url = [NSURL fileURLWithPath:path];
    //视频播放对象
    MPMoviePlayerController *movie = [[MPMoviePlayerController alloc] initWithContentURL:url];
    movie.controlStyle = MPMovieControlStyleFullscreen;
    [movie.view setFrame:self.view.bounds];
    movie.initialPlaybackTime = -1;
    [self.view addSubview:movie.view];
    //注册一个播放结束的通知, 当播放结束时, 监听到并且做一些处理
    //播放器自带有播放通知的功能, 在此仅仅只需要注册观察者监听通知的即可
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:movie];
    [movie play];
}

- (void)myMovieFinishedCallback:(NSNotification *)notify
{
    //视频播放对象
    MPMoviePlayerController *theMovie = [notify object];
    //销毁播放通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:(theMovie)];
    //释放视频对象
    [theMovie.view release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
